<template>
  <el-dialog :title="form.id ? '问卷详情' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="问卷名称" prop="name">
            <el-input v-model="form.name" :disabled="true" placeholder="请输入表单名称" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="24" class="mb20" v-for="(item, index) in form.questions" key="index">
          <view>
            <view style="display: flex;align-items: center">
              <view style="margin-right: 10px;width: 120px;font-size: 18px"> 第 {{ index + 1 }} 题</view>
              <el-radio-group style="width: 80%;" v-model="item.type" @change="(val) => {
                handleChange(val, index);
              }">
                <el-radio :key="index" :label="item.value" border v-for="(item, index) in type_Flag">{{ item.label }}
                </el-radio>
              </el-radio-group>

              <view style="display: flex;align-items: center;">
                <el-button type="primary" @click="addQuestion(index)" :disabled="loading">下方新增</el-button>
                <el-button type="danger" @click="delQuestion(index)" :disabled="loading">删除</el-button>
              </view>
            </view>
            <el-input style="margin-top: 10px;" v-model="item.question" placeholder="请输入问题" />
            <el-input style="margin-top: 5px;" v-model="item.data" v-if="item.type == 1" :disabled="true"
              placeholder="简答题无需输入" />
            <el-input style="margin-top: 5px;" v-model="item.data" v-if="item.type == 2" placeholder="请输入选项,每个选项逗号隔开" />
            <el-input style="margin-top: 5px;" v-model="item.data" v-if="item.type == 3" placeholder="请输入选项,每个选项逗号隔开" />
          </view>
        </el-col> -->
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="NaireFormDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/backoffice/naireform'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  name: '',
  questions: [{ type: 1, question: '', data: '' }]
});
const type_Flag = ref([{
  label: '简答',
  value: 1
}, {
  label: '单选',
  value: 2
}, {
  label: '多选',
  value: 3
}])
// 定义校验规则
const dataRules = ref({
  name: [{ required: true, message: '表单名称不能为空', trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取naireForm信息
  if (id) {
    form.id = id
    getnaireFormData(id)
  } else {
    form.questions = [{ type: 1, question: '', data: '' }]
  }
};
const handleChange = (label, e) => {
  if (label == 1) {
    form.questions[e].data = ''
  }

}
const addQuestion = (index) => {
  let newArr = { type: 1, question: '', data: '' }
  form.questions.splice(index + 1, 0, newArr)
}
const delQuestion = (index) => {
  if (form.questions.length < 0) {
    useMessage().error("只剩一个问题了！");
    return
  }
  form.questions.splice(index, 1)
}
// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const getnaireFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>